'use strict';
$(document).ready(function () {

  function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg); // 匹配目标参数
        if (r != null)
            return r[2];
        return null; // 返回参数值
  }

  var indexPage = function () {
    var methods = {
      // 轮盘动画
      initAnimation: function initAnimation() {
        $('.lpImg').addClass('lpAnimation');
        setTimeout(function () {
          $(".money>img").css('opacity', '1').css('width', '100%').css('height', '100%');
        }, 1000);
      },
      // 下载
      download: function download() {
        if (/i(os|phone|pad)/i.test(navigator.userAgent)) {
          // ios
          window.location.href = 'https://itunes.apple.com/cn/app/%E6%9C%89%E9%92%B1%E8%AE%B0-%E8%AE%B0%E8%B4%A6app/id1434811529?mt=8';
        } else if (/android/i.test(navigator.userAgent)) {
          // 安卓
        }
      }
    };
    var o = {
      init: function init() {
        methods.initAnimation();
      }
    };
    return o;
  }();
  // indexPage.init();
  var baseUrl = "http://api.oouye.com";
  //var baseUrl = "http://local.tengxin.com:10055";
  var flag = false;
  var channel = getUrlParam('c');
  
  $.ajax({
    type: "GET",
    url: baseUrl + "/log/url/" + channel,
    success: function (res) {
      
    }
  });

  $(".getCode").click(function () {
    var mobile = $("#tel").val();
    if (mobile == '') {
      alert('请输入您的手机号码');
      return false;
    }
    if (flag == false) {
      flag = true;
      $.ajax({
        type: "GET",
        dataType: "json",
        data: {
          phone: mobile,
          domain: 'xiaozhusudai'
        },
        url: baseUrl + "/account/app_code",
        success: function (res) {
          if (res.code !== 10200) {
            flag = false;
            alert(res.msg);
            return false;
          }
          var diff = 120;
          var clock = setInterval(function () {
            if (diff == 0) {
              flag = false;
              $(".getCode").html("获取验证码");
              clearInterval(clock);
              return;
            }
            $(".getCode").html('剩余' + diff-- + '秒');
          }, 1000);
        }
      });
    }
  });

  var tip = false;

  if (tip == false) {
    $(".btn").click(function () {
      tip = true;

      var mobile = $("#tel").val();
      if (mobile == '') {
        alert('请输入您的手机号码');
        tip = false;
        return false;
      }
      var code = $("#code").val();
      if (code.length !== 4) {
        alert('请输入正确的验证码');
        tip = false;
        return false;
      }
      $.ajax({
        type: "POST",
        dataType: "json",
        data: {
          phone: mobile,
          code: code,
          boundleId: "com.lin.xiaozhusudai",
          key: channel
        },
        url: baseUrl + "/register",
        success: function (res) {
          tip = false;
          if(res.code === 10302){
            if(res.data.indexOf('mopai') > 0){
              //zip包中的下载页面
              window.location.href = 'download.html';
            }else{
               window.location.href = res.data;
            }
          }else if (res.code === 10200) {
            window.location.href = 'download.html';
          }else {
            alert(res.msg);
          }
          
        }
      });
    });
  }
});
